<?php
/**
 * Created by PhpStorm.
 * User: 何健喔喔喔
 * Date: 2016/12/10
 * Time: 14:29
 */


    define('TOKEN','hejian');
    $obj=new weiXin1();
//只有在公众平台上的验证存在echostr，区分微信客户端
    if(!isset($_GET['echostr'])){
       $obj->recieve();
    }else{
        $obj->checkSignature();
    }
    //$obj->checkSignature();
    //$obj->recieve();

class weiXin1{
    public function checkSignature()
{
    $signature = $_GET["signature"];
    $timestamp = $_GET["timestamp"];
    $nonce = $_GET["nonce"];
    
    $token = TOKEN;
    $tmpArr = array($token, $timestamp, $nonce);
    sort($tmpArr, SORT_STRING);
    $tmpStr = implode( $tmpArr );
    $tmpStr = sha1( $tmpStr );
    
    if( $tmpStr == $signature ){
        echo $_GET['echostr'];//输出与之匹配
        return true;
    }else{
        return false;
    }
}
    public function recieve(){
        //$obj=file_get_contents("php://input");
        $obj=$GLOBALS['HTTP_RAW_POST_DATA'];//得到xml格式的输入
       /* $obj="<xml>
 <ToUserName><![CDATA[hejian]]></ToUserName>
 <FromUserName><![CDATA[alibaba]]></FromUserName>
 <CreateTime>1348831860</CreateTime>
 <MsgType><![CDATA[text]]></MsgType>
 <Content><![CDATA[this is a test]]></Content>
 <MsgId>12345678901234561111</MsgId>
 </xml>";*/
       
        $obj=simplexml_load_string($obj,"SimpleXMLElement",LIBXML_NOCDATA);
        //var_dump($obj->MsgType);
        //$result=$this->recieveText($obj);
        //var_dump($result);
        //die();
        $this->logger("接受到：".$obj);
        if(!trim(empty($obj))){
            switch ($obj->MsgType){
                case "text":
                    $result=$this->recieveText($obj);
                    break;
                case "event":
                    $result=$this->receiveEvent($obj);
                    break;
                    
    

        }
            if(!empty($result)){
                echo $result;
        
            }else{
                $xml="<xml>
                             <ToUserName><![CDATA[%s]]></ToUserName>
                             <FromUserName><![CDATA[%s]]></FromUserName>
                             <CreateTime>%s</CreateTime>
                             <MsgType><![CDATA[%s]]></MsgType>
                             <Content><![CDATA[%s]]></Content>
                             <MsgId>%s</MsgId>
                             </xml>";
                $result=sprintf($xml,$obj->FromUserName,$obj->ToUserName,time(),$obj->MsgType,"没有这条文本消息",$obj->MsgId);
                echo $result;
            }
    }        }
    public function recieveText($obj){
        $content=trim($obj->Content);
        //var_dump(strstr($content,"HK"));
        //die();
        if(strstr($content,"hi")){
            $xml="<xml>
                     <ToUserName><![CDATA[%s]]></ToUserName>
                     <FromUserName><![CDATA[%s]]></FromUserName>
                     <CreateTime>%s</CreateTime>
                     <MsgType><![CDATA[%s]]></MsgType>
                     <Content><![CDATA[%s]]></Content>
                     <MsgId>%s</MsgId>
                     </xml>";
            $result=sprintf($xml,$obj->FromUserName,$obj->ToUserName,time(),$obj->MsgType,"您的消息接收到了",$obj->MsgId);
            //return $result;
            //die();
        }elseif (strstr($content,"a")){
            $result=$this->recieveImg($obj);
        }elseif (strstr($content,"s")){
            $result=$this->receiveImgs($obj);
        }elseif (strstr($content,"图片")){
            $result=$this->receiveMedia($obj);
        }
        $this->logger($result);
        return $result;
    }
    public function recieveImg($obj){
        $xml="<xml>
            <ToUserName><![CDATA[%s]]></ToUserName>
            <FromUserName><![CDATA[%s]]></FromUserName>
            <CreateTime>%s</CreateTime>
            <MsgType><![CDATA[%s]]></MsgType>
            <ArticleCount>1</ArticleCount>
            <Articles>
            <item>
            <Title><![CDATA[%s]]></Title> 
            <Description><![CDATA[%s]]></Description>
            <PicUrl><![CDATA[%s]]></PicUrl>
            <Url><![CDATA[%s]]></Url>
            </item>
            </Articles>
            </xml>";
        $result=sprintf($xml,$obj->FromUserName,$obj->ToUserName,time(),"news","this is a title","there are  decriptions","http://s1.51cto.com/wyfs02/M00/8A/E0/wKioL1g-Z2CALGNcAAEST50ssqA411.jpg","http://developer.51cto.com/art/200909/150121.htm");
        return $result;
    }
    public function receiveImgs($obj){
        $content=array();
        $content[]=array("Title"=>"this is a title","Description"=>"there are  decriptions","PicUrl"=>"http://s1.51cto.com/wyfs02/M00/8A/E0/wKioL1g-Z2CALGNcAAEST50ssqA411.jpg","Url"=>"http://developer.51cto.com/art/200909/150121.htm");
        $content[]=array("Title"=>"this is a title","Description"=>"there are  decriptions","PicUrl"=>"http://s1.51cto.com/wyfs02/M00/8A/E0/wKioL1g-Z2CALGNcAAEST50ssqA411.jpg","Url"=>"http://developer.51cto.com/art/200909/150121.htm");
        $content[]=array("Title"=>"this is a title","Description"=>"there are  decriptions","PicUrl"=>"http://s1.51cto.com/wyfs02/M00/8A/E0/wKioL1g-Z2CALGNcAAEST50ssqA411.jpg","Url"=>"http://developer.51cto.com/art/200909/150121.htm");
        $str="<item>
            <Title><![CDATA[%s]]></Title> 
            <Description><![CDATA[%s]]></Description>
            <PicUrl><![CDATA[%s]]></PicUrl>
            <Url><![CDATA[%s]]></Url>
            </item>";
        $news="";
        foreach ($content as $newArray){
            $news.=sprintf($str,$newArray['Title'],$newArray['Description'],$newArray['PicUrl'],$newArray['Url']);
        }
        
        
        
        $xml="<xml>
            <ToUserName><![CDATA[%s]]></ToUserName>
            <FromUserName><![CDATA[%s]]></FromUserName>
            <CreateTime>%s</CreateTime>
            <MsgType><![CDATA[%s]]></MsgType>
            <ArticleCount>%s</ArticleCount>
            <Articles>
                $news
            </Articles>
            </xml>";
    
        $result=sprintf($xml,$obj->FromUserName,$obj->ToUserName,time(),"news",count($newArray));
        return $result;
        
        
    }
    public function logger($content){
        $logsize=100000;
        $log="logger.txt";
        if(file_exists($log)&&filesize($log)>$logsize){
            unlink($log);
        }else{
            file_put_contents($log,date('H:i:s')."".$content."\n",FILE_APPEND);
        }
    }
    
  /*  public function sendTmp(){
        //1获取到access_token
        
        $url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
        //2组装数组
        $array=$array(
            'touser'=>'',
            'template_id',
            'data'=>$array(
                
        )
        )
        //3数据->json
        //4调用curl函数
        
    }*/
  public function receiveEvent($obj){
      $xml="<xml>
                     <ToUserName><![CDATA[%s]]></ToUserName>
                     <FromUserName><![CDATA[%s]]></FromUserName>
                     <CreateTime>%s</CreateTime>
                     <MsgType><![CDATA[%s]]></MsgType>
                     <Content><![CDATA[%s]]></Content>
                     <MsgId>%s</MsgId>
                     </xml>";
      $result=sprintf($xml,$obj->FromUserName,$obj->ToUserName,time(),"text","欢迎加入Ueana",$obj->MsgId);
      return $result;
  }
  
  
  public function receiveMedia($obj){
      $xml="<xml>
            <ToUserName><![CDATA[%s]]></ToUserName>
            <FromUserName><![CDATA[%s]]></FromUserName>
            <CreateTime>%s</CreateTime>
            <MsgType><![CDATA[%s]]></MsgType>
            <Image>
            <MediaId><![CDATA[%s]]></MediaId>
            </Image>
            </xml>";
      $result=sprintf($xml,$obj->FromUserName,$obj->ToUserName,time(),"image","dBoY3vYBTTnInEzvVrnhurDR2FHMSvyvCsuhqzee2pE5rVxz40DbrJPlRctBHdNI");
      return $result;
    
    
  }
}
